﻿using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_SmartParking_Base;
using WpfApp_SmartParking_Entities;
using WpfApp_SmartParking_IService;
using WpfApp_SmartParking_Models;
using WpfApp_SmartParking_Service;

namespace WpfApp_SmartParking.ViewModels.Dialog;

public class MenuDetailViewModel : DetailViewModelBase
{
    public MenuItemModel MenuItemData { get; set; } = new MenuItemModel();
    

    public List<SysMenu> ParentNodes { get; set; } =
                new List<SysMenu>();

    private IMenuService _menuService;

    private IEventAggregator _eventAggregator;


    public MenuDetailViewModel(IMenuService menuService,IEventAggregator eventAggregator)
    {
        _menuService = menuService;

        _eventAggregator = eventAggregator;

        ParentNodes = _menuService.Query<SysMenu>(m => m.MenuType == 1)
                                      .ToList();
        ParentNodes.Insert(0, new SysMenu { MenuHeader = "ROOT", MenuId = 0 });

        SubmitCommand = new DelegateCommand(DoSave);

        //取消
        CancelCommand = new DelegateCommand(() => 
        {
            DialogResult dialogResult = new DialogResult();
            base.CloseWindow(dialogResult);
        });
    }

    public override bool CanCloseDialog()
    {
        return true;
    }

    public override void OnDialogClosed()
    {
        
    }

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        MenuItemModel? _MenuItemData = parameters.GetValue<MenuItemModel?>("MenuModel");
        if (_MenuItemData == null)
        {
            Title = "新增菜单";
            MenuItemData.MenuId = -1;
        }
        else
        {
            Title = "编辑菜单";
            MenuItemData.MenuId = _MenuItemData.MenuId;
            MenuItemData.MenuHeader = _MenuItemData.MenuHeader;
            MenuItemData.ParentId = _MenuItemData.ParentId;
            MenuItemData.MenuType = _MenuItemData.MenuType;
            MenuItemData.MenuIcon = _MenuItemData.MenuIcon;
            MenuItemData.TargetView = _MenuItemData.TargetView;
        }
    }

    private void DoSave()
    {
        if (MenuItemData.MenuId == -1) //新增
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            uint key = Convert.ToUInt32(ts.TotalSeconds);
            _menuService.Insert<SysMenu>(new SysMenu
            {
                MenuId = (int)key,// 新增Id
                MenuHeader = MenuItemData.MenuHeader,
                MenuIcon = MenuItemData.MenuIcon,
                TargetView = MenuItemData.TargetView,
                ParentId = MenuItemData.ParentId,
                MenuType = MenuItemData.MenuType,
                State = 1
            });
        }
        else 
        {
            var entity = _menuService.Find<SysMenu>(MenuItemData.MenuId);
            entity.MenuHeader = MenuItemData.MenuHeader;
            entity.MenuIcon = MenuItemData.MenuIcon;
            entity.TargetView = MenuItemData.TargetView;
            entity.ParentId = MenuItemData.ParentId;
            entity.MenuType = MenuItemData.MenuType;
            _menuService.Update<SysMenu>(entity);
        }
        _eventAggregator.GetEvent<RefreshMenuEvent>().Publish();
        base.CloseWindow(new DialogResult(ButtonResult.OK));
    }
}
